//
//  XYLFenceMapAnnotationView.m
//  XYLBaiduMap2017
//
//  Created by 夏玉林 on 17/2/16.
//  Copyright © 2017年 夏玉林. All rights reserved.
//

#import "XYLFenceMapAnnotationView.h"


@interface XYLFenceMapAnnotationView()

@property (nonatomic, strong) CALayer *colorDotLayer;

@property (nonatomic, strong) CALayer *outerDotLayer;

@end

@implementation XYLFenceMapAnnotationView

//!外圈
- (CALayer*)outerDotLayer {
    if(!_outerDotLayer) {
        _outerDotLayer = [CALayer layer];
        _outerDotLayer.bounds = self.bounds;
        _outerDotLayer.contents = (id)[self circleImageWithColor:self.outerColor height:self.bounds.size.height].CGImage;
        _outerDotLayer.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
        _outerDotLayer.contentsGravity = kCAGravityCenter;
        _outerDotLayer.contentsScale = [UIScreen mainScreen].scale;
        _outerDotLayer.shadowColor = [UIColor blackColor].CGColor;
        _outerDotLayer.shadowOffset = CGSizeMake(0, 2);
        _outerDotLayer.shadowRadius = 3;
        _outerDotLayer.shadowOpacity = 0.3;
        _outerDotLayer.shouldRasterize = YES;
        _outerDotLayer.rasterizationScale = [UIScreen mainScreen].scale;
    }
    return _outerDotLayer;
}
//!内圈
- (CALayer*)colorDotLayer
{
    if(!_colorDotLayer)
    {
        _colorDotLayer = [CALayer layer];
        CGFloat width = self.bounds.size.width-6;
        _colorDotLayer.bounds = CGRectMake(0, 0, width, width);
        _colorDotLayer.allowsGroupOpacity = YES;
        _colorDotLayer.backgroundColor = self.annotationColor.CGColor;
        _colorDotLayer.cornerRadius = width/2;
        _colorDotLayer.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
            
            if(self.delayBetweenPulseCycles != INFINITY) {
                CAMediaTimingFunction *defaultCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
                
                CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
                animationGroup.duration = self.pulseAnimationDuration;
                animationGroup.repeatCount = INFINITY;
                animationGroup.removedOnCompletion = NO;
                animationGroup.autoreverses = YES;
                animationGroup.timingFunction = defaultCurve;
                animationGroup.speed = 1;
                animationGroup.fillMode = kCAFillModeBoth;
                
                CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.xy"];
                pulseAnimation.fromValue = @0.8;
                pulseAnimation.toValue = @1;
                pulseAnimation.duration = self.pulseAnimationDuration;
                
                CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
                opacityAnimation.fromValue = @0.8;
                opacityAnimation.toValue = @1;
                opacityAnimation.duration = self.pulseAnimationDuration;
                
                animationGroup.animations = @[pulseAnimation, opacityAnimation];
                
                dispatch_async(dispatch_get_main_queue(), ^(void) {
                    [_colorDotLayer addAnimation:animationGroup forKey:@"pulse"];
                });
            }
        });
        
    }
    return _colorDotLayer;
}

#pragma mark - =====================UIView方法====================
- (id)initWithAnnotation:(id<BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
#pragma mark============在这里修改定位点的 frame===============
        self.bounds = CGRectMake(0, 0, 22, 22);
        
        self.pulseAnimationDuration = 0;
        
        self.delayBetweenPulseCycles = 0;
        
        self.annotationColor = [UIColor colorWithRed:0.000 green:0.478 blue:1.000 alpha:1];
        
        self.outerColor = [UIColor whiteColor];
    }
    return self;
}

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    if (newSuperview)
        [self reloadLayers];
}

- (void)reloadLayers
{
    [_outerDotLayer removeFromSuperlayer];
    
    _outerDotLayer = nil;
    
    [self.layer addSublayer:self.outerDotLayer];
    
    [_colorDotLayer removeFromSuperlayer];
    
    _colorDotLayer = nil;
    
    [self.layer addSublayer:self.colorDotLayer];
}

- (UIImage*)circleImageWithColor:(UIColor*)color height:(float)height {
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(height, height), NO, 0);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    UIBezierPath* fillPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, height, height)];
    [color setFill];
    [fillPath fill];
    
    UIImage *dotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGColorSpaceRelease(colorSpace);
    
    return dotImage;
}


@end
